<template>
  <div class="app-container">
    <el-form :model="queryParams"
             ref="queryForm" size="small"
             :inline="true" v-show="showSearch"
             label-width="68px" style="float: left">
      <el-form-item label="患者姓名" prop="patientName">
        <el-input
          v-model="queryParams.patientName"
          placeholder="请输入患者姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="patientIdentityNo">
        <el-input
          v-model="queryParams.patientIdentityNo"
          placeholder="请输入患者身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="患者状态" prop="patientStatus">
        <el-select v-model="queryParams.patientStatus" placeholder="请选择患者状态" clearable>
          <el-option
            v-for="dict in dict.type.patient_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button
      type="primary"
      plain
      icon="el-icon-plus"
      size="mini"
      @click="handleAdd"
      style="float: right;"
    >新增
    </el-button>

    <el-table v-loading="loading" :data="patientList" @selection-change="handleSelectionChange" :default-sort = "{prop: 'patientNo', order: 'descending'}">
      <el-table-column label="序号" type="index" width="50" align="center"/>
<!--      <el-table-column label="患者编号" align="center" prop="patientNo" sortable/>-->
      <el-table-column label="患者身份证号" align="center" prop="patientIdentityNo"/>
      <el-table-column label="患者姓名" align="center" prop="patientName">
        <template slot-scope="scope">
          <router-link :to="'/patient/detail/' + scope.row.patientId" class="link-type">
            <span>{{ scope.row.patientName }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column label="患者年龄" align="center" prop="patientAge"/>
      <el-table-column label="患者性别" align="center" prop="patientGender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.patientGender"/>
        </template>
      </el-table-column>
      <el-table-column label="联系方式" align="center" prop="patientPhone"></el-table-column>
      <el-table-column label="患者状态" align="center" prop="patientStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.patient_status" :value="scope.row.patientStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-folder"
            @click="register(scope.row)"
          >登记
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
          >详情
          </el-button>
          <br/>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-folder-checked"
            @click="archive(scope.row)"
          >归档
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {listPatient, getPatient, delPatient, addPatient, updatePatient, insertPatient} from "@/api/gaoxin/patient";
import Recorder from "js-audio-recorder";
import {addClinic, listClinic, updateClinic} from "@/api/gaoxin/clinic";
import {getUserAll} from "@/api/system/user";
import {listTaskalive} from "@/api/gaoxin/taskdata";
import dayjs from "@/api/gaoxin/dayjs.min";

export default {
  name: "Patient",
  dicts: ['patient_status','sys_user_sex','marital_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 患者表格数据
      patientList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 患者查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patientNo: null,
        patientName: null,
        patientBirthday: null,
        patientAge: null,
        patientGender: null,
        patientIdentityNo: null,
        patientJob: null,
        patientWeight: null,
        patientHeight: null,
        patientFolk: null,
        patientAddress: null,
        patientPhone: null,
        patientMarriage: null,
        patientStatus: null,
      },
      // 表单参数
      form: {},
      isActive: true,

    };
  },
  created() {
    this.getList();
    this.getDicts("marital_status").then(res=>{
      this.marital_status=res.data;
    })
  },
  methods: {
    /** 查询患者列表 */
    getList() {
      this.loading = true;
      // this.queryParams.patientStatus = '1'; //患者未就诊
      listPatient(this.queryParams).then(response => {
        // console.log(response.rows);
        this.patientList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getInfo() {
      getUserAll().then(response => {
        this.userOptions = response.users;
        // console.log('userOptions',this.userOptions)
      });
    },
    register(row) {
      // 就诊中无法登记
      if (row.patientStatus == 0) {
        // console.log("row",row)
        this.$modal.msgWarning("该患者正在就诊，无法进行登记");
      } else {
        let patientId = row.patientId;
        this.$router.push("/workflow/register/add/" + patientId);
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        patientId: null,
        patientNo: null,
        patientName: null,
        patientBirthday: null,
        patientAge: null,
        patientGender: null,
        patientIdentityNo: null,
        patientJob: null,
        patientWeight: null,
        patientHeight: null,
        patientFolk: null,
        patientAddress: null,
        patientPhone: null,
        patientMarriage: null,
        patientStatus: "0",
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 详情按钮操作 */
    patdetail(row) {
      // console.log('row是啥：',row)
      var temp = {
        name: 'patient_detail',
        params: {
          pat: row
        }
      }
      this.$router.push(temp)
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.patientId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push("/workflow/register/new");
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      let patientId = row.patientId;
      this.$router.push("/patient/detail/" + patientId);
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      const name = row.patientName;
      const status = row.patientStatus;
      if(status === '1') {
        this.$message.warning(`患者未就诊，无法修改`);
        return
      }
      let patientId = row.patientId;
      this.$router.push("/patient/update/" + patientId);
    },

    archive(row) {
      if (row.patientStatus == 1) {
        this.$modal.msgWarning("患者未就诊，无法归档");
      }
      let patientId = row.patientId;
      listClinic({patientId: patientId, status: '0'}).then(response => {
        // console.log('response.rows',response.rows)
        let clinic = response.rows;
        let clinicId = clinic[0].clinicId;
        // console.log('clinicId',clinicId);
        listTaskalive({clinicId:clinicId}).then(response=>{
          let taskList = response.rows;
          // console.log('taskList',taskList)
          for(let i=0;i<taskList.length;i++){
            if(taskList[i].status<3){
              this.isActive=false
              break
            }
          }
          if(!this.isActive){
            this.$alert('该患者仍有任务未审核，无法归档', '提示', {
              confirmButtonText: '确定',
              callback: action => {
                this.$message({
                  type: 'info',
                  message: `已取消归档`
                });
              }
            });
          }else {
            this.$confirm('该患者的任务均已审核，确认归档吗?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              let params = {
                clinicId: clinicId,
                outTime: dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss'),
                status: '1'
              }
              updateClinic(params).then(() => {
                this.$tab.refreshPage();
                this.$message({
                  type: 'success',
                  message: '归档成功!'
                });
                updatePatient({patientId: patientId,patientStatus:'1'}).then(() => {
                  // this.$modal.msgSuccess("归档成功");
                  this.getList();
                });
              })
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消出院'
              });
            })
          }
        });
      });
    },
  }
};
</script>
